<?php

/**
 * Class for work with database table "books"
 * 
 * @version 2012-10-22
 * @package Db model
 * @subpackage accounts
 * @uses Db_Table
 * @category Db model
 */

class PagesModel extends Db_Table{

 /**
     * __construct
     * This construct function connects to the table "books".
     * 
     * @version 2012-10-22
     * @uses parent::__construct('books')
     * @return object
     */
	function __construct()
	{
		parent::__construct("pages");
	}
	
	
	/**    
     * getMaxId
     * This function returns a total number of members from the database.
     *
     * @version 2012-10-24 
     * @param array $where
     * @return int $result['cnt_members']
     * @access public
     */
	public function getMaxIdBook() {
		$sql = "SELECT MAX(book_id) as max_book_id FROM ".$this->table." WHERE book_id < 8500";
		$result = $this->db->getField($sql);
		return (int)$result;	
	}	
	
	/**    
     * getMinIdBook
     * This function returns a total number of members from the database.
     *
     * @version 2012-10-24 
     * @param array $where
     * @return int $result['cnt_members']
     * @access public
     */
	public function getMinIdBook() {
		$sql = "SELECT MIN(book_id) as max_book_id FROM ".$this->table." WHERE book_id > 8500";
		$result = $this->db->getField($sql);
		return (int)$result;	
	}	
	
	/**    
     * getMaxId
     * This function returns a total number of members from the database.
     *
     * @version 2012-10-24 
     * @param array $where
     * @return int $result['cnt_members']
     * @access public
     */
	public function getMaxNumPage($book_id = 0) {
		$book_id = (int)$book_id;
		$sql = "SELECT MAX(num_page) as max_num_page FROM ".$this->table." WHERE book_id = ".$book_id;
		$result = $this->db->getField($sql);
		return (int)$result;	
	}
	
	public function getPage($book,$page){
		$sql = "SELECT p.book_id,p.num_page,p.text,(SELECT MAX(num_page) FROM pages AS p2 WHERE p2.book_id = p.book_id) AS cnt_page FROM pages AS p WHERE book_id=".intval($book)." AND num_page=".intval($page)." LIMIT 1";
		return $this->db->getRow($sql);
	}
}
